//
//  RecycleHistoryViewController.m
//  BaseFrame
//
//  Created by ENERGY on 2018/5/2.
//  Copyright © 2018年 com.mxh. All rights reserved.
//

#import "RecycleHistoryViewController.h"
#import "RecycleCollectionViewCell.h"
#import "RecycleSectionHeaderCollectionReusableView.h"
#import "RecycleSectionFooterCollectionReusableView.h"
@interface RecycleHistoryViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)UICollectionView * collectionView;
@end

@implementation RecycleHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton * rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60/WIDTH_5S_SCALE, 20/WIDTH_5S_SCALE)];
    [rightBtn addTarget:self action:@selector(rightBarButtonItemClick) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:@"清除" forState:UIControlStateNormal];
    [rightBtn setBackgroundColor:[UIColor clearColor]];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = DEF_FontSize_14;
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self.view addSubview:self.collectionView];
}

#pragma mark - init
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
        layout.sectionInset = UIEdgeInsetsMake(0/WIDTH_5S_SCALE, 10/WIDTH_5S_SCALE, 0/WIDTH_5S_SCALE, 10/WIDTH_5S_SCALE);
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[RecycleCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([RecycleCollectionViewCell class])];
        [_collectionView registerClass:[RecycleSectionHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([RecycleSectionHeaderCollectionReusableView class])];
        [_collectionView registerClass:[RecycleSectionFooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([RecycleSectionFooterCollectionReusableView class])];
    }return _collectionView;
}

#pragma mark - collectionView的delegate和dataSource
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RecycleCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([RecycleCollectionViewCell class]) forIndexPath:indexPath];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 9;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:@"UICollectionElementKindSectionFooter"]) {
        RecycleSectionFooterCollectionReusableView * footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([RecycleSectionFooterCollectionReusableView class]) forIndexPath:indexPath];
        return footerView;
    }else{
        RecycleSectionHeaderCollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([RecycleSectionHeaderCollectionReusableView class]) forIndexPath:indexPath];
        return headerView;
    }
}

-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection: (NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH, 50/WIDTH_5S_SCALE);
}

-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection: (NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH, 16/WIDTH_5S_SCALE);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    int collectionViewH = (SCREEN_WIDTH - 50/WIDTH_5S_SCALE)/4;
    return CGSizeMake(collectionViewH, collectionViewH);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10/WIDTH_5S_SCALE;
}

#pragma mark - action
- (void)rightBarButtonItemClick{
    NSLog(@"清除");
}

@end
